package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.player.UserInfo;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class NianTu extends BaseRepair {

    public NianTu(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        List<Map<String, String>> listMap = new ArrayList<>();
        String dateStart = "2022-10-15";
        String dateEnd = "2022-10-18";
        listMap = getLogNianTu(dateStart,dateEnd);
        listMap = getLogBoxNianTu(listMap,dateStart,dateEnd);
        DataMgr.addData(listMap);
    }

    private List<Map<String, String>> getLogNianTu(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,sum(num) as allNum,goodsId from " +
                " t_l_goods_2 t" +
                " where userId in(select DISTINCT(userId) from t_l_login_detail where time >= ?) and goodsId = 120898 and isGet = 0 and way != 230071" +
                " and date >= ? and date <= ?" +
                " group by userId,goodsId;";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,dateStart);
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("消耗粘土数量",checkNullToEmpty(rs.getString("allNum")));
                UserInfo userInfo = getUserInfo(temp.get("userId"));
                temp.put("vipExp",String.valueOf(userInfo.getVipExp()));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLogBoxNianTu(List<Map<String, String>> odataList,String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        Map<Long,List<Map<String, String>>> checkMap = new ConcurrentHashMap<>();
        Map<Long,Integer> checkCountMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT" +
                "        userId," +
                "        goodsId," +
                "        sum(num) AS num1," +
                "        (CASE" +
                " WHEN goodsId = 110040 THEN" +
                "        sum(num) / 1" +
                " WHEN goodsId = 110014 THEN" +
                "        sum(num) / 1" +
                " WHEN goodsId = 110112 THEN" +
                "        sum(num) / 1" +
                " WHEN goodsId = 110017 THEN" +
                "        sum(num) / 2" +
                " WHEN goodsId = 119019 THEN" +
                "        sum(num) / 3" +
                " ELSE 0" +
                " END )AS num2" +
                " FROM" +
                "        t_l_goods_2 t" +
                " WHERE" +
                "        userId = ?" +
                " AND goodsId IN (" +
                "        110040," +
                "        110014," +
                "        110112," +
                "        110017," +
                "        119019" +
                " )" +
                " AND date >= ?" +
                " AND date <= ?" +
                " AND way = 1110003" +
                " GROUP BY" +
                "        userId," +
                "        goodsId;";
        try {
            for(Map<String,String> odata : odataList){
                call = getGameServer().getLogConnection().prepareCall(sql);
                int i=1;
                call.setString(i++,odata.get("userId"));
                call.setString(i++,dateStart);
                call.setString(i++,dateEnd);
                rs = call.executeQuery();
                Long userId = Long.valueOf(odata.get("userId"));
                while (rs.next()){
                    if(!checkMap.containsKey(userId)){
                        checkMap.put(userId,new ArrayList<>());
                    }
                    Map<String,String> adata = new ConcurrentHashMap<>();
                    adata.put("userId",rs.getString("userId"));
                    adata.put("goodsId",rs.getString("goodsId"));
                    adata.put("num1",rs.getString("num1"));
                    adata.put("num2",rs.getString("num2"));
                    checkMap.get(userId).add(adata);

                    checkCountMap.put(userId,checkCountMap.getOrDefault(userId,0) + rs.getInt("num2") );
                }
                getLogger().error("cccccc:{}",checkMap.get(userId).size());
                if(!checkCountMap.getOrDefault(userId,0).equals(Integer.valueOf(odata.get("消耗粘土数量")))){
                    String getStr = "";
                    for(Map<String, String> gData : checkMap.get(userId)){
                        getStr = getStr + gData.get("goodsId") + "=" + gData.get("num1") + ";";
                    }
                    odata.put("开出宝箱数量",checkCountMap.getOrDefault(userId,0) + "");
                    odata.put("获得总道具",getStr);
                    dataList.add(odata);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }

        return dataList;
    }

    private int getUserVip(String userId){
        int maxVip = 0;
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,max(nowValue) as maxVip from t_l_vip_exp WHERE userId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setString(1, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                maxVip = rs.getInt("maxVip");
                return maxVip;
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }

        return maxVip;
    }

}
